package com.cq.hd.order.render.dto;

import com.cq.hd.common.enums.biz.OrderItemTypeEnum;
import com.cq.hd.order.api.dto.CreateOrderDto;
import com.cq.hd.order.render.OrderContext;
import com.cq.hd.product.api.vo.ActiveVo;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * 价格明细
 *
 * @author much
 * @since 2024/09/12 23:08
 */
@Data
public class PriceDetailDto {

    // 折扣比例
    private BigDecimal discountRate;

    // 根据折扣比例算的优惠金额
    private BigDecimal discountAmt;

    // 红包金额
    private BigDecimal redPacketAmt;

    // 活动价格
    private BigDecimal activePrice;

    // 订单金额
    private BigDecimal orderPrice;

    // 付款金额
    private BigDecimal payPrice;

    // 商品总价
    private BigDecimal totalGoodsPrice;

    // 签到费
    private BigDecimal totalSignInFee;

    // 商家订单收入
    private BigDecimal orderIncomeAmt;

    // 用户可得积分
    private BigDecimal userPointsAmt;

    public void reCalculateTotalGoodsPrice(OrderContext orderContext) {
        // 设置商品总价
        BigDecimal totalGoodsPrice = orderContext.getActiveGoods().stream()
                .map(e -> e.getGoodsPrice().multiply(BigDecimal.valueOf(e.getGoodsNum())))
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        CreateOrderDto createOrderDto = orderContext.getCreateOrderDto();
        // 再乘以订单份数
        totalGoodsPrice = totalGoodsPrice.multiply(BigDecimal.valueOf(createOrderDto.getOrderNum()));
        setTotalGoodsPrice(totalGoodsPrice);

        ActiveVo activeVo = orderContext.getActiveVo();
        if (activeVo.getSignInFee() != null) {
            BigDecimal totalSignFee = activeVo.getSignInFee().multiply(BigDecimal.valueOf(createOrderDto.getOrderNum()));
            setTotalSignInFee(totalSignFee);
        }

//        BigDecimal totalSignFee = orderContext.getActiveGoods().stream()
//                .filter(e -> OrderItemTypeEnum.SIGN_IN.getValue().equals(e.getType()))
//                .map(e -> e.getGoodsPrice().multiply(BigDecimal.valueOf(e.getGoodsNum())))
//                .reduce(BigDecimal.ZERO, BigDecimal::add);
//        setTotalSignInFee(totalSignFee);
    }

}
